package com.baizhi.cmfzmanager.controller;

import com.baizhi.cmfzmanager.entity.Article;
import com.baizhi.cmfzmanager.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @RequestMapping("/getArticles")
    public Map getArticles(int page,int rows){
        return articleService.getArticles(page,rows);
    }
    @RequestMapping("/getArticlesByGuruId")
    public Map getArticlesByGuruId(int page,int rows,int guruId){
        return articleService.getArticlesByGuruId(page,rows,guruId);
    }
    @RequestMapping("/add")
    public Map add(Article article){
        Map map = new HashMap();
        try {
            articleService.insert(article);
            map.put("message","添加成功！！");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message","添加失败！！");
        }
        return map;

    }

    @RequestMapping("/update")
    public Map update(Article article){
        Map map = new HashMap();
        try {
            articleService.update(article);
            map.put("message","修改成功！！");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message","修改失败！！");
        }
        return map;
    }
    @RequestMapping("/delete")
    public Map delete(int id){
        Map map = new HashMap();
        try {
            articleService.delete(id);
            map.put("message","删除成功！！");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message","删除失败！！");
        }
        return map;
    }
}
